package com.example.opengl.render.rectangle

import android.opengl.GLES20
import android.opengl.GLES20.*
import com.android.baselibrary.app.MainApplication
import com.example.opengl.R
import com.example.opengl.base.TextResourceReader
import com.example.opengl.render.base.IDrawer
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import java.util.Arrays

/**
 * @author: Mr.You
 * @create: 2023-07-26 16:03
 * @description:
 **/
class RectangleDrawer : IDrawer {
    var mTextureId = -1
    var mVideoWidth = 0
    var mVideoHeight = 0
    var mScreenWidth = 0
    var mScreenHeight = 0

    var mVertexFloatBuffer: FloatBuffer? = null
    var mTextureFloatBuffer: FloatBuffer? = null

    var mVertexShader = -1
    var mTextureShader = -1

    var mProgram = -1;

    var mVertexCoords = floatArrayOf(
        0.5f, 0.5f,
        -0.5f, 0.5f,
        -0.5f, -0.5f,
        0.5f, -0.5f
    )
    val mTextureCoords = floatArrayOf(
        0.0f, 1.0f, 0.0f, 1.0f,
        1.0f, 0.0f, 0.0f, 1.0f,
        0.0f, 0.0f, 1.0f, 1.0f
    )

    init {
        //  initCoords()
    }



    private fun initCoords() {
        //通过宽高比，将y轴转化为和x轴长度保持一致。
        val ration = mScreenWidth / mScreenHeight.toFloat()
        val floats = Arrays.copyOf(mVertexCoords, mVertexCoords.size)
        val limit=  floats.size/2
        for (i in 0 until limit){
            floats[1+i *2]  *=ration
        }




        val bv = ByteBuffer.allocateDirect(floats.size * 4)
        bv.order(ByteOrder.nativeOrder())
        mVertexFloatBuffer = bv.asFloatBuffer()
        mVertexFloatBuffer?.put(floats)
        mVertexFloatBuffer?.position(0)

        val bt = ByteBuffer.allocateDirect(mTextureCoords.size * 4)
        bt.order(ByteOrder.nativeOrder())
        mTextureFloatBuffer = bt.asFloatBuffer()
        mTextureFloatBuffer?.put(mTextureCoords)
        mTextureFloatBuffer?.position(0)


    }

    override fun draw() {
        if (mTextureId != -1) {
            initShader()
            doDraw()
        }

    }

    private fun initShader() {
        mVertexShader = glCreateShader(GL_VERTEX_SHADER)
        glShaderSource(
            mVertexShader,
            TextResourceReader.readTextFileFormResource(
                MainApplication.mainApplication,
                R.raw.vertex_color_triangle_shader
            )
        )
        glCompileShader(mVertexShader)

        mTextureShader = glCreateShader(GL_FRAGMENT_SHADER)
        glShaderSource(
            mTextureShader, TextResourceReader.readTextFileFormResource(
                MainApplication.mainApplication,
                R.raw.fragment_color_triangle_shader
            )
        )

        glCompileShader(mTextureShader)
        mProgram = glCreateProgram()
        glAttachShader(mProgram, mVertexShader)
        glAttachShader(mProgram, mTextureShader)

        glLinkProgram(mProgram)
        glUseProgram(mProgram)

        glDeleteShader(mTextureShader)
        glDeleteShader(mVertexShader)


    }

    private fun doDraw() {

        glEnableVertexAttribArray(0)
        glEnableVertexAttribArray(1)

        glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, mVertexFloatBuffer)
        glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, mTextureFloatBuffer)
        glDrawArrays(GL_TRIANGLE_FAN, 0, 4)

    }


    override fun setTextureID(id: Int) {
        mTextureId = id
    }

    override fun release() {

    }

    override fun setWorldSize(worldW: Int, worldH: Int) {
        mScreenWidth = worldW
        mScreenHeight = worldH
        initCoords()
    }

    override fun setVideoSize(videoW: Int, videoH: Int) {
        mVideoHeight = videoH
        mVideoWidth = videoW
    }
}